package com.aube.interact.taste.model;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.aube.interact.taste.R;
import com.aube.interact.taste.controller.DataManager;
import com.huyn.baseframework.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by hill on 16/12/8.
 */

public class VoteData {
    private static String TAG = "Aube/VoteData";

    private Context mContext;

    public String mRid;

    private int mFocusedIndex = -1;
    private int mQAAnswerIndex = -1;
    private int mUserClickIndex = -1;

    private AtomicBoolean mAnswerRevealed = new AtomicBoolean(false);
    private AtomicBoolean mPresetDataLoaded = new AtomicBoolean(false);

    private HashMap<String, OptionItem> mOptionMap = new HashMap<>();
    private List<OptionItem> mOptionList = new ArrayList<>();

    public VoteData(Context context) {
        mContext = context;
    }

    public void compose(TimeLinePiece data) {
        mRid = data.getRid();

        for(int i = 0; i < data.optionSize(); i++) {
            String optionId = data.getOptions().get(i).optionId;
            mOptionMap.put(optionId, data.getOptions().get(i));
            mOptionList.add(data.getOptions().get(i));
        }
    }

    /**
     * 更新选中选项的Index
     * @param index
     */
    public void updateFocusedIndex(int index) {
        if(mFocusedIndex == index) {
            mFocusedIndex = -1;
        } else {
            mFocusedIndex = index;
        }
    }

    /**
     * 复位选中选项的Index
     * @param index
     */
    public void restorePreselectedIndex(int index) {
        if(mFocusedIndex == index)
            return;
        mFocusedIndex = index;
    }

    /**
     * 判断此选项是否被选中
     * @param index
     * @return
     */
    public boolean IsFocusedIndex(int index) {
        if(index == mFocusedIndex) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断目前是否有焦点, 即是否有被选中的选项
     * @return
     */
    public boolean IsFocused() {
        if(mFocusedIndex == -1) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 获取用户最新点击的选项index
     * @return
     */
    public int getUserClickIndex() {
        return mUserClickIndex;
    }

    public int getOptionPercent(String optionId) {
        int optionCount = DataManager.getInstance(mContext).getOptionCount(optionId);
        int totalCount = DataManager.getInstance(mContext).getTotalCount(mRid);
        if(totalCount == 0)
            return 50;
        int percent = 100 * optionCount / totalCount;
        Utils.Log(TAG, "[getOptionPercent] optionCount = " + optionCount + ", getTotalCount(mRid) = " + DataManager.getInstance(mContext).getTotalCount(mRid));
        return percent;
    }

    public int getTotalCount() {
        return DataManager.getInstance(mContext).getTotalCount(mRid);
    }

    public void update(List<OptionItem> optionItemList) {
        for(OptionItem item : optionItemList) {
            if(mOptionMap.containsKey(item.optionId)) {
                mOptionMap.remove(item.optionId);
            }
            mOptionMap.put(item.optionId, item);
        }
    }

    /**
     * 用户点击行为后的数据更新事件
     * @param index
     */
    public void update(int index) {
        mUserClickIndex = index;

        if(mFocusedIndex == index) {
            DataManager.getInstance(mContext).updateTotalCount(mRid, true);
            DataManager.getInstance(mContext).updateOptionCount(mOptionList.get(index).optionId, true);
        } else if(mFocusedIndex == -1) {
            DataManager.getInstance(mContext).updateTotalCount(mRid, false);
            DataManager.getInstance(mContext).updateOptionCount(mOptionList.get(index).optionId, false);
        } else {
            DataManager.getInstance(mContext).updateOptionCount(mOptionList.get(index).optionId, false);
            DataManager.getInstance(mContext).updateOptionCount(mOptionList.get(mFocusedIndex).optionId, true);
        }

        updateFocusedIndex(index);
        if(IsFocused()) {
            DataManager.getInstance(mContext).update(mRid, index);
        } else {
            DataManager.getInstance(mContext).update(mRid, -1);
        }
    }

    public String getReportOptionId(int index) {
        if(mFocusedIndex != index) {
            return mOptionList.get(index).optionId;
        } else {
            return "";
        }
    }

    public String getReportCancelId(int index) {
        if(mFocusedIndex == index) {
            return mOptionList.get(index).optionId;
        } else if(mFocusedIndex == -1 ){
            return "";
        } else {
            return mOptionList.get(mFocusedIndex).optionId;
        }
    }

    public int getOptionColor(int index) {
        if(index == mFocusedIndex) {
            return R.color.vote_Percent_H;
        } else if(DataManager.getInstance(mContext).IsMaxCountOption(mRid, mOptionList.get(index).optionId)) {
            return R.color.vote_Percent_M;
        } else {
            return R.color.vote_Percent_L;
        }
    }

    public Drawable getQAAnswerDrawable(int index) {
        if(!mAnswerRevealed.get()) return null;

        boolean bCorrect = mOptionList.get(index).isRightAnswer();
        if(bCorrect) {
            return mContext.getResources().getDrawable(R.drawable.icon_right);
        } else if(index == mFocusedIndex) {
            return mContext.getResources().getDrawable(R.drawable.icon_wrong);
        } else {
            return null;
        }
    }

    public boolean getQAAnswerReveal() {
        return mAnswerRevealed.get();
    }

    public void setQAAnswerReveal(boolean value) {
        mAnswerRevealed.set(value);
    }

    public boolean getPresetDataLoad() {
        return mPresetDataLoaded.get();
    }

    public void setPresetDataLoad(boolean value) {
        mPresetDataLoaded.set(value);
    }
}